package com.nameless.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nameless.dao.CarTypeMapper;
import com.nameless.entity.CarType;
import com.nameless.service.CarTypeService;
import com.nameless.vo.CarTypeVo;
import com.nameless.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：潘淼淼
 * @date ：Created in 2023/2/27 9:35
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CarTypeServiceImpl extends ServiceImpl<CarTypeMapper, CarType> implements CarTypeService {
    @Resource
    private CarTypeMapper carTypeMapper;


    //          查询
    @Override
    public Result selectList(CarTypeVo carTypeVo) {
        PageHelper.startPage(carTypeVo.getPageNum(), carTypeVo.getPageSize());
        QueryWrapper<CarType> wrapper = null;
        if (carTypeVo.getTypeName() != null) {
            wrapper = new QueryWrapper<>();
            System.out.println("if");
            wrapper.like("type_name", carTypeVo.getTypeName());
        }
        List<CarType> carTypes = carTypeMapper.selectList(wrapper);
        PageInfo<CarType> info = new PageInfo<>(carTypes);
        return new Result(200, "查询所有成功", info);
    }

    @Override
    public Result updates(CarType carType) {

        int update = carTypeMapper.updateA(carType);
        return new Result(200,"修改信息",update);
    }

    @Override
    public Result deleteById(Integer id) {
        int delete = carTypeMapper.delete(id);
        return new Result(200,"删除信息",delete);
    }

    @Override
    public Result addOne(CarType carType) {
//        自动添加时间
        carType.setAddtime(LocalDateTime.now());
        int insert = carTypeMapper.insert(carType);
        return new Result(200,"添加信息",insert);
    }

    @Override
    public Result selectLis() {
        List<CarType> carTypes = carTypeMapper.selectList(null);
        return new Result(200,"查询成功",carTypes);
    }

    @Override
    public CarType getTypeName(Integer carId) {
        CarType carType = carTypeMapper.getTypeName(carId);
        return carType;
    }
//    @Override
//    public Result selectList(Integer pageNum, Integer pageSize, String typeName) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<CarType> carTypes = carTypeMapper.queryAlls(typeName);
//        System.out.println(carTypes);
//        PageInfo<CarType> info = new PageInfo<>(carTypes);
//        return new Result(200, "查询所有成功", info);
//    }
}